import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../common/my_icon.dart';
import '../../../components/appbar_widget/appbar_widget.dart';
import '../../../components/status/empty.dart';
import '../../../components/status/load_more_state.dart';
import '../../../components/status/not_network.dart';
import '../../../config/alarm_type_config.dart';
import '../../../config/global_config.dart';
import '../../../controller/network/network_ctrl.dart';
import '../../../model/bean/alarm_bean.dart';
import '../../../model/bean/community_bean.dart';
import '../../../utils/screenutil/fit.dart';
import '../../../utils/tools/Tools.dart';
import 'ctrl/alarm_record_ctrl.dart';

class AlarmRecord extends StatelessWidget {
  // 导入控制器
  final arCtrl = Get.put(AlarmRecordCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget('告警记录').build(context),
      body: Obx(
        () {
          List<AlarmBean> _data = arCtrl.listData;
          return Container(
            height: double.infinity,
            child: Stack(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 64.f),
                  child: arCtrl.MARKSYMBOL.value
                      ? SmartRefresher(
                          footer: CustomFooter(
                            builder: (BuildContext ct, LoadStatus mode) {
                              Widget body = LoadMoreState(mode);
                              return Container(child: Center(child: body));
                            },
                          ),
                          enablePullDown: false,
                          enablePullUp: true,
                          controller: arCtrl.refreshController.value,
                          onLoading: arCtrl.onLoading,
                          child: ListView.builder(
                            itemCount: arCtrl.listData.length,
                            itemBuilder: (BuildContext c, int i) => InkWell(
                              onTap: () => arCtrl.onLeave({
                                "monitorId": arCtrl.listData[i].boxId,
                                "communityName": arCtrl.listData[i].villageName,
                                "liftId": arCtrl.listData[i].elevatorId,
                                "liftLocation":
                                    "${arCtrl.listData[i].buildingNo}/${arCtrl.listData[i].unitNo}/${arCtrl.listData[i].shaft}"
                              }),
                              child: Container(
                                padding: EdgeInsets.only(
                                    left: 20.f, top: 15.f, bottom: 15.f),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  border: 10 - 1 == i
                                      ? null
                                      : Border(
                                          bottom: BorderSide(
                                            width: 1.f,
                                            color: GlobalConfig.borderColor,
                                          ),
                                        ),
                                ),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Container(
                                      width: 60.f,
                                      height: 60.f,
                                      margin: EdgeInsets.only(right: 10.f),
                                      decoration: BoxDecoration(
                                        color: Colors.red,
                                        borderRadius:
                                            BorderRadius.circular(60.f),
                                      ),
                                      child: Icon(
                                        MyIcon.gg,
                                        size: 34.f,
                                        color: Colors.white,
                                      ),
                                    ),
                                    Container(
                                      width: 320.f,
                                      margin: EdgeInsets.only(right: 10.f),
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            "${AlarmTypeConfig.type[_data[i].type]}",
                                            style: TextStyle(
                                              color: GlobalConfig.titleColor,
                                              fontSize: 24.f,
                                            ),
                                            softWrap: true,
                                            textAlign: TextAlign.left,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                          SizedBox(height: 15.f),
                                          Text(
                                            "${_data[i].villageName}/${_data[i].buildingNo}/${_data[i].unitNo}/${_data[i].shaft}号电梯井",
                                            style: TextStyle(
                                              color: GlobalConfig.assistColor,
                                              fontSize: 20.f,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      alignment: Alignment.topRight,
                                      width: 180.f,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.end,
                                        children: [
                                          Text(
                                            "${_data[i].status == 1 ? '已处理' : '未处理'}",
                                            style: TextStyle(
                                              color: _data[i].status == 1
                                                  ? Colors.blue
                                                  : Colors.red,
                                              fontSize: 24.f,
                                            ),
                                          ),
                                          SizedBox(height: 15.f),
                                          Text(
                                            "${Tools.dateFormat(DateTime.parse(_data[i].createTime), DataFormats.mo_d_h_m)}",
                                            style: TextStyle(
                                              color: GlobalConfig.assistColor,
                                              fontSize: 20.f,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Container(
                                      width: 40.f,
                                      alignment: Alignment.centerRight,
                                      child: Icon(
                                        Icons.chevron_right,
                                        color: GlobalConfig.assistColor,
                                        size: 36.f,
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        )
                      : Container(
                          width: 640.f,
                          height: double.infinity,
                          child: Center(child: Empty()),
                        ),
                ),

                // 选项卡、条件选择器
                Positioned(
                  child: Container(
                    width: 640.f,
                    height: 65.f,
                    margin: EdgeInsets.only(top: fit(_nc.topDistance.value)),
                    decoration: BoxDecoration(
                      boxShadow: GlobalConfig.boxShadow,
                    ),
                    child: Column(
                      children: [
                        Container(
                          width: 640.f,
                          height: 64.f,
                          margin: EdgeInsets.only(top: 1.f),
                          color: Colors.white,
                          child: Flex(
                            direction: Axis.horizontal,
                            children: [
                              Container(
                                child: InkWell(
                                  onTap: () => arCtrl.getDateRange(context),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        alignment: Alignment.centerLeft,
                                        padding: EdgeInsets.only(left: 15.f),
                                        width: 275.f,
                                        child: Text(
                                          "${arCtrl.selectedDateRange.value}",
                                          softWrap: true,
                                          maxLines: 1,
                                          textAlign: TextAlign.left,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(
                                            color: arCtrl.selectedDateRange
                                                        .value ==
                                                    "请选择日期"
                                                ? GlobalConfig.assistColor
                                                : Colors.black,
                                            fontSize: 22.f,
                                          ),
                                        ),
                                      ),
                                      // Icons.chevron_right
                                      InkWell(
                                        onTap: () => arCtrl.emptyDateRange(),
                                        child: Container(
                                          width: 64.f,
                                          height: 64.f,
                                          child: Icon(
                                            arCtrl.selectedDateRange.value ==
                                                    "请选择日期"
                                                ? Icons.chevron_right
                                                : MyIcon.cuowu,
                                            color: GlobalConfig.assistColor,
                                            size: 32.f,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: 1.f,
                                child: Container(
                                  height: 60.f,
                                  color: GlobalConfig.borderColor,
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: InkWell(
                                  onTap: () async {},
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Container(
                                        width: 300.f,
                                        padding: EdgeInsets.only(
                                          left: 20.f,
                                        ),
                                        child: DropdownButtonHideUnderline(
                                          child: DropdownButton(
                                            items: arCtrl.communityData.value,
                                            value:
                                                arCtrl.selectedCommunity.value,
                                            hint: Container(
                                              width: 240.f,
                                              child: Text(
                                                "${arCtrl.selectedCommunity.value != null ? arCtrl.selectedCommunity.value.name : '请选择小区'}",
                                                softWrap: true,
                                                maxLines: 1,
                                                textAlign: TextAlign.left,
                                                overflow: TextOverflow.ellipsis,
                                                style: TextStyle(
                                                  color:
                                                      GlobalConfig.assistColor,
                                                  fontSize: 22.f,
                                                ),
                                              ),
                                            ),
                                            icon: Icon(
                                              Icons.chevron_right,
                                              color: GlobalConfig.assistColor,
                                              size: 35.f,
                                            ),
                                            onChanged: (CommunityBean val) =>
                                                arCtrl.setDropDownSelectedData(
                                                    val),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),

                // 网络状态
                Positioned(top: 0, child: NotNetwork()),
              ],
            ),
          );
        },
      ),
    );
  }
}
